package com.homedo.odin.solution.service.bizservice.personal;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.HttpClient;
import com.homedo.core.common.util.ObjectConverter;
import com.homedo.odin.solution.contract.pc.AdoptReplyReq;
import com.homedo.odin.solution.contract.pc.CloseBountyReq;
import com.homedo.odin.solution.contract.pc.GetBountyByIdResp;
import com.homedo.odin.solution.contract.pc.GetBountyListReq;
import com.homedo.odin.solution.contract.pc.GetBountyListResp;
import com.homedo.odin.solution.contract.pc.HebiResp;
import com.homedo.odin.solution.contract.pc.HighMoneyBountysResp;
import com.homedo.odin.solution.contract.pc.InsertBountyReq;
import com.homedo.odin.solution.contract.personal.treasure.AddScoreReq;
import com.homedo.odin.solution.enumcode.TemplateCodeEnum;
import com.homedo.odin.solution.enumcode.basic.FreezeEnum;
import com.homedo.odin.solution.enumcode.basic.IsEndEnum;
import com.homedo.odin.solution.enumcode.bounty.BountyAdoptEnum;
import com.homedo.odin.solution.enumcode.bounty.BountyStatusEnum;
import com.homedo.odin.solution.enumcode.bounty.BountyTypeEnum;
import com.homedo.odin.solution.enumcode.constant.ConstantUtils;
import com.homedo.odin.solution.enumcode.hebi.HebiOrderBizTypeEnum;
import com.homedo.odin.solution.enumcode.hebi.HebiOrderResultStatusEnum;
import com.homedo.odin.solution.enumcode.hebi.HebiOrderStatusEnum;
import com.homedo.odin.solution.enumcode.other.MoneyEnum;
import com.homedo.odin.solution.enumcode.other.RemarkEnum;
import com.homedo.odin.solution.enumcode.other.ScoreRuleEnum;
import com.homedo.odin.solution.model.dto.BountyBean;
import com.homedo.odin.solution.model.dto.HebiModel;
import com.homedo.odin.solution.model.dto.HebiOrderBean;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.entity.Bounty;
import com.homedo.odin.solution.model.entity.BountyReply;
import com.homedo.odin.solution.model.entity.BountyTotal;
import com.homedo.odin.solution.model.entity.BountyTotalBean;
import com.homedo.odin.solution.model.entity.HebiOrder;
import com.homedo.odin.solution.model.entity.HebiOrderLog;
import com.homedo.odin.solution.model.entity.UserBasic;
import com.homedo.odin.solution.service.BountyReplyServiceImpl;
import com.homedo.odin.solution.service.BountyServiceImpl;
import com.homedo.odin.solution.service.BountyTotalServiceImpl;
import com.homedo.odin.solution.service.HebiOrderLogServiceImpl;
import com.homedo.odin.solution.service.HebiOrderServiceImpl;
import com.homedo.odin.solution.service.MessageServiceImpl;
import com.homedo.odin.solution.service.MessageTypeServiceImpl;
import com.homedo.odin.solution.service.UserBasicServiceImpl;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * @author: donglibing
 * @date: 2018/3/7 18:58
 * @Description: 悬赏业务处理
 */
@Service
public class BountyBizService {

    @Autowired
    BountyServiceImpl impl;

    @Autowired
    BountyTotalServiceImpl bountyTotalService;

    @Autowired
    UserBasicServiceImpl userBasicService;

    @Autowired
    BountyReplyServiceImpl bountyReplyService;

    @Autowired
    MessageTypeServiceImpl messageTypeService;

    @Autowired
    MessageServiceImpl messageService;

    @Autowired
    CommonBizService commonBizService;

    @Autowired
    HebiOrderServiceImpl hebiOrderService;

    @Autowired
    HebiOrderLogServiceImpl hebiOrderLogService;

    @Autowired
    UserTreasureBizService userTreasureBizService;

    @Value("${Hebi_Host_Url}")
    private String hostUrl;

    @Value("${APPID}")
    private String appId;

    /**
     * 发布悬赏 返回主键
     */
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public int publish(InsertBountyReq req) {
        //1.数据校验
        if (req.getUserId() <= 0 || req.getAmount().compareTo(new BigDecimal(0)) < 0 || StringUtils
                .isEmpty(req.getBountyDetail()) || req.getBountyType() <= 0 || StringUtils.isEmpty(req.getTitle())) {
            throw new BizException(Meta.CODE_451.getCode(), Meta.CODE_451.getMsg());
        }
        //是否在枚举中
        if (req.getBountyType() != BountyTypeEnum.FIND_PEOPLE.getDbValue()
                && req.getBountyType() != BountyTypeEnum.FIND_SOLUTION.getDbValue()
                && req.getBountyType() != BountyTypeEnum.FIND_TECHNOLOGY_HELP.getDbValue()) {
            throw new BizException(Meta.CODE_451.getCode(), "悬赏类型非法");
        }

        //判断是否存在该uid
        UserBasic userBasic = commonBizService.validationUser(req.getUserId());
        //2.校验河马币是否足够
        HebiResp hebiResp = getHebi(userBasic.getUserId());
        if (req.getAmount().compareTo(new BigDecimal(hebiResp.getAmountNoCash().doubleValue())) > 0) {
            throw new BizException(Meta.CODE_451.getCode(), userBasic.getUserId() + "_不可提现河马币不足");
        }

        //3.更新悬赏汇总
        BountyTotalBean bountyTotalBean = bountyTotalService.getMyBountyTotal(req.getUserId());
        if (bountyTotalBean == null) {
            bountyTotalBean = new BountyTotalBean();
            bountyTotalBean.setUserId(req.getUserId());
            bountyTotalBean.setAmount(req.getAmount());
            bountyTotalBean.setUserName(userBasic.getUserName());
            bountyTotalBean.setBountyCount(1);
            bountyTotalBean.setCreatedBy(req.getUserId());
            bountyTotalBean.setModifiedBy(req.getUserId());
            BountyTotal bountyTotal = ObjectConverter.copyProperties(bountyTotalBean, BountyTotal.class);
            boolean re = bountyTotalService.insert(bountyTotal);
            if (!re) {
                throw new BizException(Meta.CODE_500.getCode(), userBasic.getUserId() + "_插入悬赏汇总失败");
            }
        } else {
            int total = bountyTotalBean.getBountyCount() + 1;
            bountyTotalBean.setBountyCount(total);
            BigDecimal amount = bountyTotalBean.getAmount().add(req.getAmount());
            bountyTotalBean.setAmount(amount);
            bountyTotalBean.setModifiedTime(new Date(System.currentTimeMillis()));
            BountyTotal bountyTotal = ObjectConverter.copyProperties(bountyTotalBean, BountyTotal.class);
            boolean re1 = bountyTotalService.updateById(bountyTotal);
            if (!re1) {
                throw new BizException(Meta.CODE_500.getCode(), userBasic.getUserId() + "_更新悬赏汇总失败");
            }

        }

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        //4.发布
        Bounty bountyPublish = ObjectConverter.copyProperties(req, Bounty.class);
        bountyPublish.setUserName(userBasic.getUserName());
        bountyPublish.setStatus(BountyStatusEnum.NOW.getDbValue());
        bountyPublish.setAmountStatus(FreezeEnum.FREEZE.getDbValue());
        bountyPublish.setCreatedBy(req.getUserId());
        bountyPublish.setModifiedBy(req.getUserId());
        bountyPublish.setUuid(uuid);
        boolean result = impl.insert(bountyPublish);
        if (!result) {
            throw new BizException(Meta.CODE_500.getCode(), userBasic.getUserId() + "_发布悬赏失败");
        }
        // 增加积分
        AddScoreReq addScoreReq = new AddScoreReq();
        addScoreReq.setBizCode(bountyPublish.getId().toString());
        addScoreReq.setType(ScoreRuleEnum.Bounty.getType());
        addScoreReq.setUserId(req.getUserId());
        commonBizService.addScore(addScoreReq);

        //5.更新流水订单表
        HebiOrder hebiOrder = new HebiOrder();
        hebiOrder.setCreatedBy(req.getUserId());
        hebiOrder.setModifiedBy(req.getUserId());
        hebiOrder.setUuid(uuid);
        hebiOrder.setStatus(HebiOrderStatusEnum.FREEZE.getDbValue());
        hebiOrder.setResultStatus(HebiOrderResultStatusEnum.SUCCESS.getDbValue());
        hebiOrder.setBizId(bountyPublish.getId().toString());
        hebiOrder.setUserId(req.getUserId());
        hebiOrder.setDiscountAmount(req.getAmount());
        hebiOrder.setHebiAmount(req.getAmount());
        hebiOrder.setBizType(HebiOrderBizTypeEnum.BOUNTY.getDbValue());
        boolean result1 = hebiOrderService.insert(hebiOrder);
        if (!result1) {
            throw new BizException(Meta.CODE_500.getCode(), userBasic.getUserId() + "_更新流水订单表失败");
        }

        //插入订单流水日志
        HebiOrderLog newHebiOrderLog = ObjectConverter.copyProperties(hebiOrder, HebiOrderLog.class);
        newHebiOrderLog.setRemark(RemarkEnum.PublishBounty.getMsg());
        boolean re = hebiOrderLogService.insert(newHebiOrderLog);
        if (!re) {
            throw new BizException(Meta.CODE_500.getCode(), "新建悬赏插入订单流水日志失败");
        }

        //6.支出河币
        String url = hostUrl + "/IntegralService/Integral/Minus";
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("UsingType", MoneyEnum.BountyPayment.getCode());
        params.add("AppId", appId);

        params.add("SerialNumber", uuid);
        params.add("Type", MoneyEnum.BountyPayment.getMsg());
        params.add("AccountId", userBasic.getUserId().toString());
        params.add("Amount", req.getAmount().toString());
        params.add("Remark", "赏金支付（支出）");
        //发送http请求并返回结果
        String reslutJson = HttpClient.post(url, params);
        JSONObject jsonObject = JSONObject.fromObject(reslutJson);
        if (!(boolean) jsonObject.get(ConstantUtils.HebiConstants.IS_OK)) {
            throw new BizException(Meta.CODE_500.getCode(), userBasic.getUserId() + "_河币扣除失败");
        }

        return bountyPublish.getId();
    }


    /**
     * 高赏金悬赏
     */
    public HighMoneyBountysResp highMoneyBountys(int top) {
        HighMoneyBountysResp resp = new HighMoneyBountysResp();
        List<Bounty> bountyList = impl.highMoneyBountys(top);
        resp.setHighBountyList(ObjectConverter.copyList(bountyList, BountyBean.class));
        return resp;
    }

    /**
     * 获取悬赏列表
     */
    public GetBountyListResp getBountyList(GetBountyListReq req) {
        GetBountyListResp resp = new GetBountyListResp();
        PageBean pageBean = new PageBean(req.getPageNum(), req.getPageSize());
        if (req.getStartTime() == null || req.getEndTime() == null) {
            req.setStartTime(Long.valueOf(0));
            req.setEndTime(Long.valueOf(0));
        }
        if (req.getStatus() == null) {
            req.setStatus("");
        }
        PageInfo<BountyBean> bountyList = impl
                .getBountyList(pageBean, req.getUserId(), req.getBountyType(), req.getStatus(), req.getStartTime(),
                        req.getEndTime());
        resp = BeanUtils.transfrom(GetBountyListResp.class, bountyList);
        return resp;
    }


    /**
     * 根据id获取悬赏详情
     */
    public GetBountyByIdResp checkBounty(int id) {
        GetBountyByIdResp resp = new GetBountyByIdResp();
        Bounty bounty = impl.checkBounty(id);
        if (bounty == null) {
            throw new BizException(Meta.CODE_451.getCode(), "不存在该悬赏");
        }
        UserBasic userBasic = commonBizService.validationUser(bounty.getUserId());
        int count = bounty.getPageView() + 1;
        bounty.setPageView(count);
        resp.setBounty(ObjectConverter.copyProperties(bounty, BountyBean.class));

        BountyTotalBean bountyTotalBean = bountyTotalService.getMyBountyTotal(bounty.getUserId());
        if (bountyTotalBean == null) {
            resp.setBountyCount(0);
            resp.setRate(BigDecimal.valueOf(0));
        } else {
            resp.setBountyCount(bountyTotalBean.getBountyCount());
            resp.setRate(bountyTotalBean.getRate());
        }

        resp.setUserName(userBasic.getUserName());
        resp.setAvatar(bountyTotalBean.getUserAvatar());

        //更新浏览量
        impl.updateById(bounty);
        return resp;
    }

    /**
     * 关闭任务
     */
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public void close(CloseBountyReq req) {

        //1.数据校验
        if (req.getBountyId() <= 0 || req.getUserId() <= 0) {
            throw new BizException(Meta.CODE_451.getCode(), Meta.CODE_451.getMsg());
        }
        //判断是否有该用户
        UserBasic userBasic = commonBizService.validationUser(req.getUserId());
        //判断是否存在该悬赏
        Map<String, Object> map1 = new HashMap<>();
        map1.put("user_id", req.getUserId());
        map1.put("is_del", 0);
        map1.put("id", req.getBountyId());
        map1.put("status", BountyStatusEnum.NOW.getDbValue());
        List<Bounty> bountys = impl.selectByMap(map1);
        if (bountys == null || bountys.size() == 0) {
            throw new BizException(Meta.CODE_451.getCode(), userBasic.getUserId() + "_根据条件未查询到该悬赏");
        }
        Bounty bounty = bountys.get(0);

        //2.更改悬赏状态
        bounty.setAmountStatus(FreezeEnum.UNFREEZE.getDbValue());
        bounty.setStatus(BountyStatusEnum.CLOSED.getDbValue());
        bounty.setIsEnd(IsEndEnum.END.getDbValue());
        boolean re = impl.updateById(bounty);
        if (!re) {
            throw new BizException(Meta.CODE_500.getCode(), userBasic.getUserId() + "_更新悬赏状态异常");
        }
        //3.更改悬赏汇总
        BountyTotalBean bountyTotalBean = bountyTotalService.getMyBountyTotal(req.getUserId());
        if (bountyTotalBean == null) {
            throw new BizException(Meta.CODE_500.getCode(), userBasic.getUserId() + "_不存在用户悬赏汇总");
        }
        BigDecimal amount = bountyTotalBean.getAmount().subtract(bounty.getAmount());
        bountyTotalBean.setAmount(amount);
        BountyTotal bountyTotal = ObjectConverter.copyProperties(bountyTotalBean, BountyTotal.class);
        boolean re2 = bountyTotalService.updateById(bountyTotal);
        if (!re2) {
            throw new BizException(Meta.CODE_500.getCode(), userBasic.getUserId() + "_更新用户悬赏汇总失败");
        }

        //4.更新订单状态
        Map<String, Object> map2 = new HashMap<>();
        map2.put("uuid", bounty.getUuid());
        map2.put("status", HebiOrderStatusEnum.FREEZE.getDbValue());
        List<HebiOrder> hebiOrders = hebiOrderService.selectByMap(map2);
        if (hebiOrders == null || hebiOrders.size() == 0) {
            throw new BizException(Meta.CODE_500.getCode(), "未查询到该订单");
        }
        HebiOrder hebiOrder = hebiOrders.get(0);
        hebiOrder.setStatus(HebiOrderStatusEnum.RESTITUTION.getDbValue());
        boolean result = hebiOrderService.updateById(hebiOrder);
        if (!result) {
            throw new BizException(Meta.CODE_500.getCode(), userBasic.getUserId() + "_更新订单状态失败");
        }

        //插入订单流水日志
        HebiOrderLog newHebiOrderLog = ObjectConverter.copyProperties(hebiOrder, HebiOrderLog.class);
        newHebiOrderLog.setRemark(RemarkEnum.ClosedBounty.getMsg());
        boolean re1 = hebiOrderLogService.insert(newHebiOrderLog);
        if (!re1) {
            throw new BizException(Meta.CODE_500.getCode(), "新建关闭悬赏插入订单流水日志失败");
        }

        //5.返还河币
        String url = hostUrl + "/IntegralService/Integral/Refund";
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("AppId", appId);
        params.add("SerialNumber", bounty.getUuid());
        params.add("AccountId", userBasic.getUserId().toString());
        String reslutJson = HttpClient.post(url, params);
        JSONObject jsonObject = JSONObject.fromObject(reslutJson);
        if (!(boolean) jsonObject.get(ConstantUtils.HebiConstants.IS_OK)) {
            throw new BizException(Meta.CODE_500.getCode(), userBasic.getUserId() + "_返还河币失败");
        }

    }

    /**
     * 采纳答案
     */
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public void adopt(AdoptReplyReq req) {

        //1.数据校验
        if (req.getBountyReplyId() <= 0 || req.getUserId() <= 0) {
            throw new BizException(Meta.CODE_451.getCode(), Meta.CODE_451.getMsg());
        }
        //判断是否存在该uid
        UserBasic userBasic = commonBizService.validationUser(req.getUserId());
        //判断回复记录
        Map<String, Object> map = new HashMap<>();
        map.put("id", req.getBountyReplyId());
        map.put("is_del", 0);
        map.put("is_adopt", BountyAdoptEnum.NOT_ADOPT.getDbValue());
        List<BountyReply> bountyReplies = bountyReplyService.selectByMap(map);
        if (bountyReplies == null || bountyReplies.size() == 0) {
            throw new BizException(Meta.CODE_451.getCode(), "根据条件未查询到符合条件的悬赏回复");
        }

        //2.更新悬赏状态
        BountyReply bountyReplyBean = bountyReplies.get(0);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("id", bountyReplyBean.getBountyId());
        map2.put("is_del", 0);
        map2.put("user_id", req.getUserId());
        List<Bounty> bounties = impl.selectByMap(map2);
        if (bounties == null || bounties.size() == 0) {
            throw new BizException(Meta.CODE_451.getCode(), "根据条件未查询到符合条件的悬赏");
        }
        bountyReplyBean.setIsAdopt(BountyAdoptEnum.IS_ADOPT.getDbValue());
        boolean result = bountyReplyService.updateById(bountyReplyBean);
        if (!result) {
            throw new BizException(Meta.CODE_500.getCode(), "更新悬赏回复状态异常");
        }
        Bounty bounty = bounties.get(0);
        bounty.setReplyId(bountyReplyBean.getId());
        bounty.setIsEnd(IsEndEnum.END.getDbValue());
        bounty.setStatus(BountyStatusEnum.USE.getDbValue());
        bounty.setAmountStatus(FreezeEnum.UNFREEZE.getDbValue());
        boolean result1 = impl.updateById(bounty);
        if (!result1) {
            throw new BizException(Meta.CODE_500.getCode(), "更新悬赏状态异常");
        }
        //更改被采纳人的悬赏汇总
        BountyTotalBean bountyTotalBean = bountyTotalService.getMyBountyTotal(bountyReplyBean.getUserId());
        if (bountyTotalBean == null) {
            bountyTotalBean = new BountyTotalBean();
            bountyTotalBean.setUserId(bountyReplyBean.getUserId());
            bountyTotalBean.setUserName(bountyReplyBean.getUserName());
            bountyTotalBean.setCreatedBy(bountyReplyBean.getUserId());
            bountyTotalBean.setModifiedBy(bountyReplyBean.getUserId());
            bountyTotalBean.setOwnAmount(bounty.getAmount());
            BountyTotal bountyTotal = ObjectConverter.copyProperties(bountyTotalBean, BountyTotal.class);
            boolean re = bountyTotalService.insert(bountyTotal);
            if (!re) {
                throw new BizException(Meta.CODE_500.getCode(), "插入悬赏汇总失败");
            }
        } else {
            BigDecimal amount = bountyTotalBean.getOwnAmount().add(bounty.getAmount());
            bountyTotalBean.setOwnAmount(amount);
            BountyTotal bountyTotal = ObjectConverter.copyProperties(bountyTotalBean, BountyTotal.class);
            boolean re2 = bountyTotalService.updateById(bountyTotal);
            if (!re2) {
                throw new BizException(Meta.CODE_500.getCode(), "更新用户悬赏汇总失败");
            }
        }

        //更新悬赏人的悬赏汇总
        BountyTotalBean bountierTotal = bountyTotalService.getMyBountyTotal(bounty.getUserId());
        if (bountierTotal == null) {
            throw new BizException(Meta.CODE_500.getCode(), "不存在悬赏发起人的悬赏汇总");
        }
        int adoptCount = bountierTotal.getAdoptCount() + 1;
        bountierTotal.setAdoptCount(adoptCount);
        BountyTotal bountyTotal1 = ObjectConverter.copyProperties(bountierTotal, BountyTotal.class);
        boolean re3 = bountyTotalService.updateById(bountyTotal1);
        if (!re3) {
            throw new BizException(Meta.CODE_500.getCode(), "更新悬赏人汇总失败");
        }
        //更新悬赏发布者的订单状态
        Map<String, Object> map4 = new HashMap<>();
        map4.put("uuid", bounty.getUuid());
        List<HebiOrder> hebiOrders = hebiOrderService.selectByMap(map4);
        if (hebiOrders == null || hebiOrders.size() == 0) {
            throw new BizException(Meta.CODE_500.getCode(), "未查询到该订单");
        }
        HebiOrder hebiOrder = hebiOrders.get(0);
        HebiOrderBean hebiOrderBean = ObjectConverter.copyProperties(hebiOrder, HebiOrderBean.class);
        hebiOrder.setStatus(HebiOrderStatusEnum.ROLLOUT.getDbValue());

        boolean result2 = hebiOrderService.updateById(hebiOrder);
        if (!result2) {
            throw new BizException(Meta.CODE_500.getCode(), "更新悬赏发布者的订单状态失败");
        }

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        //插入回复者的订单流水日志
        hebiOrderBean.setStatus(HebiOrderStatusEnum.PROFIT.getDbValue());
        hebiOrderBean.setUserId(bountyReplyBean.getUserId());
        hebiOrderBean.setUuid(uuid);
        hebiOrderBean.setUserId(bountyReplyBean.getUserId());
        hebiOrderBean.setModifiedBy(bountyReplyBean.getUserId());
        hebiOrderBean.setCreatedBy(bountyReplyBean.getUserId());
        hebiOrderBean.setCreatedTime(null);
        hebiOrderBean.setModifiedTime(null);
        hebiOrderBean.setFromUserId(bounty.getUserId());
        HebiOrderLog newHebiOrderLog = ObjectConverter.copyProperties(hebiOrderBean, HebiOrderLog.class);
        newHebiOrderLog.setRemark("领取悬赏河币");
        boolean re1 = hebiOrderLogService.insert(newHebiOrderLog);
        if (!re1) {
            throw new BizException(Meta.CODE_500.getCode(), "插入领取悬赏河币订单流水日志失败");
        }

        //插入被采纳者的订单状态
        HebiOrder adopter = ObjectConverter.copyProperties(hebiOrderBean, HebiOrder.class);
        boolean result3 = hebiOrderService.insert(adopter);
        if (!result3) {
            throw new BizException(Meta.CODE_500.getCode(), "插入被采纳者的订单流水失败");
        }

        //4.发送信息
        Map<String, String> map3 = new HashMap<>();
        map3.put("@@userName", bountyReplyBean.getUserName());
        map3.put("@@bounty", bounty.getTitle());
        map3.put("@@money", bounty.getAmount().toString());
        map3.put("@@bizId", bounty.getId().toString());
        commonBizService.sentMessage(map3, bountyReplyBean.getUserId(), 0, TemplateCodeEnum.bounty_use.toString());

        //5.河币转入到采纳人
        String url = hostUrl + "/IntegralService/Integral/Add";

        //post请求
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("AppId", appId);
        params.add("CashEnable", "false");
        params.add("SerialNumber", uuid);
        params.add("Type", MoneyEnum.BountyAdopt.getMsg());
        params.add("Amount", bounty.getAmount().toString());
        params.add("AccountId", bountyReplyBean.getUserId().toString());
        params.add("Remark", "悬赏采纳河币(收入)");
        //发送http请求并返回结果
        String reslutJson = HttpClient.post(url, params);
        JSONObject jsonObject = JSONObject.fromObject(reslutJson);
        if (!(boolean) jsonObject.get(ConstantUtils.HebiConstants.IS_OK)) {
            throw new BizException(Meta.CODE_500.getCode(), "河币转入到采纳人失败");
        }

    }

    /**
     * 获取河币
     */
    public HebiResp getHebi(int id) {

        //判断是否存在该uid
        UserBasic userBasic = commonBizService.validationUser(id);

        //获取河币
        String url = hostUrl + "/IntegralService/Integral/Info";
        //post请求
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("AppId", appId);
        params.add("AccountId", userBasic.getUserId().toString());
        //发送http请求并返回结果
        String reslutJson = HttpClient.post(url, params);
        JSONObject jsonObject = JSONObject.fromObject(reslutJson);
        Object data = jsonObject.get("data");
        if (!(boolean) jsonObject.get(ConstantUtils.HebiConstants.IS_OK)) {
            throw new BizException(Meta.CODE_500.getCode(), "河币转入到采纳人失败");
        }
        String jsonStr = data.toString();
        HebiModel hebiModel = JSON.parseObject(jsonStr, new TypeReference<HebiModel>() {
        });

        HebiResp resp = new HebiResp();
        resp.setAmount(hebiModel.getAmount());
        resp.setAccountId(hebiModel.getAccountId());
        resp.setAmountCash(hebiModel.getAmountCash());
        resp.setAmountNoCash(hebiModel.getAmountNoCash());
        return resp;
    }


}
